package com.slkj.shilixiaoyuanapp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.slkj.shilixiaoyuanapp.R;


/**
 * 分割线
 */

public class DividerItemDecoration extends RecyclerView.ItemDecoration{
    private Paint mPaint;
    private int mDividerHeight = 2;//分割线高度，默认为1px
    private int marginLeft;
    private int marginRight;

    /**
     * 默认分割线：高度为1px，颜色为灰色
     * @param context
     * @param orientation 列表方向
     */
    public DividerItemDecoration(Context context, int orientation) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(context.getResources().getColor(R.color.color_dddddd));
        mPaint.setStyle(Paint.Style.FILL);
    }

    /**
     * 横向左右边距
     * @param marginLeft
     * @param marginRight
     */
    public DividerItemDecoration(Context context, int marginLeft, int marginRight) {
        this(context, LinearLayoutManager.VERTICAL);
        this.marginLeft = marginLeft;
        this.marginRight = marginRight;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(0, 0, 0, mDividerHeight);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; i++) {
            /**
             * TODO：可以根据item的type进行不同的绘制
             */
            int type = parent.getAdapter().getItemViewType(i);
            View view = parent.getChildAt(i);
            float top = view.getBottom();
            float bottom = view.getBottom() + mDividerHeight;
            float left = parent.getPaddingLeft() +view.getPaddingLeft()+marginLeft;
            float right = parent.getWidth() - parent.getPaddingRight() - view.getPaddingRight() - marginRight;
            c.drawRect(left, top, right, bottom, mPaint);
        }


    }

}
